﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：MyInformation.ascx.cs
//
//
//文件功能描述：顾客的资料信息....
//
//
//创建标识：2007.11.19 李正平
//创建描述：
//
//
//修改标识：2007.11.28 李正平
//修改描述：
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JXMSTC.SmartBusiness.Common.Model;
using System.IO;
using System.Xml;
using System.Text;

public partial class Common_Controls_myInformation : System.Web.UI.UserControl
{
    //
    //异步调用保存顾客信息
    //
    protected localhost.CustomerService WSUpdateCustomerInfo = new localhost.CustomerService();

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!this.IsPostBack)
            {
                //
                //先把省市绑出
                //
                LoadXMLProvince();

                //
                //页面初始化
                //
                LoadPage();


            }      
    }

    /// <summary>
    /// 初始化页面
    /// </summary>
    protected void LoadPage()
    {
        if (Session["CustomerID"] != null)
        {
            //
            //获取顾客登录ID
            //
            string customerID = (string)Session["CustomerID"];

            //
            //新建一个顾客实例 
            //
            localhost.Customer customer = new localhost.Customer();

            //
            //获取顾客详细信息
            //
            customer = new localhost.CustomerService().GetCustomerDetailInfo(customerID);

            //
            //获取顾客的登录名
            //
            txtUserName.Text = customer.Account;

            //
            //获取顾客的第一个名字
            //
            txtFirstName.Text = customer.FirstName;

            //
            //获取顾客的最后一个名字
            //
            txtLastName.Text = customer.LastName;

            //
            //获取顾客的电话
            //
            txtPhone.Text = customer.Phone;

            //
            //获取顾客的邮件
            //
            txtEmail.Text = customer.Email;

            //
            //获取顾客的地址
            //
            txtAddress.Text = customer.Address;

            //
            //获取顾客所在地邮编
            //
            txtPostCode.Text = customer.Zip;


            dropProvince.Items.FindByText(customer.Provice).Selected = true;
            //
            //获取顾客所在省
            //
            dropProvince.SelectedItem.Text = customer.Provice;

            //
            //绑定市
            //
            BindCity();

            //
            //获取顾客所在市
            //
            dropCity.SelectedItem.Text = customer.City;
        }
    }
    /// <summary>
    /// 保存修改信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {

        //
        //获取顾客的登录ID
        //
        string customerID = (string)Session["CustomerID"];

        //
        //根据顾客ID获取顾客的信息，再对其进行修改，新建一个顾客,保存顾客输入的注册信息
        //
        localhost.Customer newCustomer = new localhost.CustomerService().GetCustomerDetailInfo(customerID);

        
        newCustomer.FirstName = txtFirstName.Text.Trim();

        newCustomer.LastName = txtLastName.Text.Trim();

        newCustomer.Phone = txtPhone.Text.Trim();

        newCustomer.Provice = dropProvince.SelectedItem.Text;

        newCustomer.City = dropCity.SelectedItem.Text;

        newCustomer.Email = txtEmail.Text.Trim();

        newCustomer.Address = txtAddress.Text.Trim();

        newCustomer.Zip = txtPostCode.Text.Trim();

        try
        {
            //
            //异步调用保存顾客信息
            //
            
            WSUpdateCustomerInfo.UpdateCustomerInfoCompleted += new localhost.UpdateCustomerInfoCompletedEventHandler(newCustomer_UpdateCustomerInfoCompleted);

            //
            //修改顾客信息结果
            //
            WSUpdateCustomerInfo.UpdateCustomerInfoAsync(newCustomer);
        }
        catch(Exception ex)
        {
            //
            //异常
            //
            lblMessage.Text = "信息保存失败,请重新修改并保存!";
            throw ex;
        }
    }


    /// <summary>
    /// 修改顾客信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void newCustomer_UpdateCustomerInfoCompleted(object sender, localhost.UpdateCustomerInfoCompletedEventArgs e)
    {
        //
        //异步操作过程异常
        //
        if (e.Error == null)
        {
            //
            //异步操作没有被取消
            //
            if (!e.Cancelled)
            {
                //
                //判断修改信息是否成功
                //
                bool success = e.Result;
                if (success)
                {
                    //
                    //修改成功  
                    //
                    lblMessage.Text = "恭喜您，资料修改成功！";
                }
                else
                {
                    //
                    //修改失败 
                    //
                    lblMessage.Text = "抱歉，资料修改失败，请重新修改！";
                }

            }
        }
        else
        {
            //
            //修改失败 
            //
            lblMessage.Text = "抱歉，系统错误！";
            throw e.Error;
        }
    }


    /// <summary>
    ///取消修改顾客信息 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        //
        //取消修改顾客信息
        //
        WSUpdateCustomerInfo.CancelAsync(null);
    }

    /// <summary>
    /// 从XML文件中绑定出省市
    /// </summary>
    protected void LoadXMLProvince()
    {
        //
        //文件名
        //
        string dataFile = "~/XML/CartInfo/ProvinceList.xml";
        StreamReader sdrXML = new StreamReader(Server.MapPath(dataFile));
        XmlDataDocument dataDoc = new XmlDataDocument();

        //
        //创建该对象为了读取XML
        //
        dataDoc.DataSet.ReadXml(sdrXML);

        //
        //读取XML文件内容
        //
        dropProvince.DataSource = dataDoc.DataSet.Tables[0].DefaultView;

        dropProvince.Items.Insert(0, "请选择省");
        dropProvince.DataTextField = "ProvinceName";
        dropProvince.DataValueField = "ProvinceID";
        dropProvince.DataBind();

    }


    /// <summary>
    /// 绑定省份
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dropProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        //
        //绑定市
        //
        BindCity();
    }

    /// <summary>
    /// 绑定市
    /// </summary>
    void BindCity()
    {
        //
        //获得选择的省份
        //
        string strProvince = dropProvince.SelectedValue;

        //
        //获得所选省份的市
        //
        string strXMLCity = "~/XML/CartInfo/CityList.xml";
        StreamReader sdrXMLCity = new StreamReader(Server.MapPath(strXMLCity));
        XmlDataDocument dataDoc = new XmlDataDocument();
        dataDoc.DataSet.ReadXml(sdrXMLCity);

        //
        //过滤条件
        //
        DataView dvCity = dataDoc.DataSet.Tables[0].DefaultView;
        dvCity.RowFilter = "ProvinceID=" + strProvince;

        //
        //绑定输出
        //
        dropCity.DataSource = dvCity;
        dropCity.DataValueField = "CityID";
        dropCity.DataTextField = "CityName";
        dropCity.DataBind();
    }

}
